<?php
class Twitter_Model_Twitter implements Twitter_Model_Mainable  {
    
    public function main(array $variables = null) {
        
        require_once WEB_ROOT_FOLDER."twitteroauth/twitteroauth.php";
        if(isset($_GET['oauth_token'])){
            $twitter_connection = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
            $twitter_connection->host = "https://api.twitter.com/1.1/";
            $access_token = $twitter_connection->getAccessToken($variables['get']['oauth_verifier']);
            $_SESSION['acces_token'] = $access_token;
            unset($_SESSION['oauth_token']);
            unset($_SESSION['oauth_token_secret']);
            if(200 == $twitter_connection->http_code){
                $twitter_access['user_access_token'] = $access_token['oauth_token'];
                $twitter_access['user_secret_token'] = $access_token['oauth_token_secret'];
                $twitter_access['user_id'] = $access_token['user_id'];
                $twitter_access['user_name'] = $access_token['screen_name'];
                $_SESSION['twitter_access'] = $twitter_access;
                
            }
        }
        else {
            $twitter_connection = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
            $twitter_connection->host = "https://api.twitter.com/1.1/";
            $request_token = $twitter_connection->getRequestToken("http://dev.twonee.com");
            $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
            $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
            switch($twitter_connection->http_code){
                case "200":
                    $url = $twitter_connection->getAuthorizeURL($token);
                    echo '<meta http-equiv="Refresh" content="0;url='.$url.'">'; 
                    break;
                default: 
                    echo "Could not connect!";
            }
        }
    }

}
